/*
 * Copyright (C) 2017 Gautam Chibde
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.chibde;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.audio.AudioWaver;

/**
 * Base class that contains common implementation for all
 * visualizers.
 * Created by gautam chibde on 28/10/17.
 */
public abstract class BaseVisualizer extends Component {
    protected byte[] bytes;
    protected Paint paint;
    protected int color;
    private AudioWaver audioWaver;

    /**
     * constructor
     *
     * @param context context
     */
    public BaseVisualizer(Context context) {
        super(context);
        init(null);
        init();
    }

    /**
     * constructor
     *
     * @param context context
     * @param attrs attrs
     */
    public BaseVisualizer(Context context, AttrSet attrs) {
        super(context, attrs);
        init(attrs);
        init();
    }

    /**
     * constructor
     *
     * @param context  context
     * @param attrs attrs
     * @param defStyleAttr defStyleAttr
     */
    public BaseVisualizer(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
        init();
    }

    private void init(AttrSet attributeSet) {
        paint = new Paint();
    }

    /**
     * init
     */
    protected abstract void init();

    /**
     * Set color to visualizer with color resource id.
     *
     * @param color color resource id.
     */
    public void setColor(int color) {
        this.color = color;
        this.paint.setColor(new Color(color));
    }

    /**
     * setPlayer
     *
     * @param sessionid sessionid
     * @param name name
     */
    public void setPlayer(int sessionid, String name) {
        audioWaver = new AudioWaver(sessionid, name);
        audioWaver.setActivated(false);
        audioWaver.setDataSize(AudioWaver.getMaxDataSize());
        audioWaver.setWaveDataObserver(new AudioWaver.WaveDataObserver() {
            @Override
            public void onWaveData(byte[] bytes, int i) {
                BaseVisualizer.this.bytes = bytes;
                getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        invalidate();
                    }
                });
            }
        }, AudioWaver.getMinInterval());
        audioWaver.setActivated(true);
    }

    @Override
    public void release() {
        super.release();

        // will be null if setPlayer hasn't yet been called
        if (audioWaver == null) {
            return;
        }
        audioWaver.release();
        bytes = null;
        invalidate();
    }

    public AudioWaver getVisualizer() {
        return audioWaver;
    }
}
